"use strict";

const logger = log.getLogger("jobs");
const commonService = requireService("common");
const jobs_path = getSysConfig("jobsPath");

/**
 * 内部统计业务 sdk 业务接口
 */
function onGet(ctx) {

    var query = ctx.url.split("?");
    var req_files;
    query = query[1] || "";
    if (query) {
        // 只取第一组参数
        query = query.split("&")[0];
        req_files = query.split(",");
    } else {
        req_files = [];
    }
    req_files.unshift("collect");
    req_files.unshift("base");
    req_files.push("boot");

    var re = "";

    req_files.forEach(function(file){
        var dat = commonService.cache.get(file);
        if (!dat) {
            dat = commonService.read(jobs_path, file+".js", logger);
            if (dat) {
                commonService.cache.set(file, dat);
            }
        }
        re += "\n"+dat;
    });

    re = commonService.warpRespone(re);

    commonService.setHeader.call(ctx);

    ctx.body = re;
}

module.exports = {
    "/getjob": ["GET", onGet]
}
